package com.suncnpap.system.controller;

import com.suncnpap.system.model.GroupType;
import com.suncnpap.system.model.Holiday;
import com.suncnpap.system.model.School;
import com.suncnpap.system.service.GroupTypeService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;


import com.app.util.beanutil.BeanUtil;
import com.app.util.common.Constants;
import com.app.util.common.EasyuiGrid;
import com.app.util.common.HandlerResult;
import com.app.util.common.ResponseUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;

/** 
 * 说明：组别Controller
 */
@Controller
@RequestMapping(value="/system/groupType")
public class GroupTypeController extends BaseController {
	
	//private static final Logger log = Logger.getLogger(GroupTypeController.class);
	
	/**
	*列表视图
	*/
	private static final String PageList = "system/groupType/list";
	
	/**
	*新增视图
	*/
	private static final String PageAdd = "system/groupType/add";
	
	/**
	*修改视图
	*/
	private static final String PageUpdate = "system/groupType/update";
	
	/**
	*查看视图
	*/
	private static final String PageView = "system/groupType/view";
	
	
	@Autowired
	GroupTypeService groupTypeService;
	
	
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
	
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			page = groupTypeService.list(page);
			result = this.returnPage(result, page);
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add",method=RequestMethod.POST)
	public void add(@RequestBody GroupType obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				init(obj);
				groupTypeService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 修改处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody GroupType obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				groupTypeService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					groupTypeService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				GroupType obj = groupTypeService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
}
